package com.cjt.chat.controller;

import com.cjt.chat.pojo.Group;
import com.cjt.chat.pojo.GroupMessage;
import com.cjt.chat.pojo.User;
import com.cjt.chat.service.GroupService;
import com.cjt.chat.service.MessageService;
import com.cjt.chat.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cjt
 * @since 2024/3/29
 */
@RestController
public class GroupController {
    @Resource
    private GroupService groupService;
    @Resource
    private UserService userService;
    @Resource
    private MessageService messageService;

    //用户请求群列表
    @GetMapping(path = "/groupList")
    public List<Group> getGroupList(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user != null) {
            return groupService.getGroupList(user.getUserid());
        }
        return null;
    }

    //用户请求群聊历史记录
    @GetMapping(path = "/groupMessageHistory")
    public Map<String, Object> getGroupMessageHistory(HttpSession session, Integer groupId, Integer page) {
        Map<String, Object> resultMap = new HashMap<>();
        User user = (User) session.getAttribute("user");
        if (user != null) {
            resultMap.put("messageData", groupService.getGroupMessage(groupId, page));
            resultMap.put("role", groupService.getMemberRole(user.getUserid(), groupId));
            return resultMap;
        }
        return null;
    }

    @GetMapping(path = "/searchGroup")
    public Group getGroupInfo(Integer groupId, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user != null) {
            return groupService.searchGroupInfo(user.getUserid(), groupId);
        }
        return null;
    }

    @GetMapping(path = "/addGroupMember")
    public void addGroupMember(Integer groupId, Integer messageId, Integer memberId, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user != null) {
            System.out.println(groupId + " " + memberId);
            groupService.addGroupMember(groupId, memberId);
            messageService.deleteMessageById(messageId);
        }
    }

    @GetMapping(path = "/getMemberList")
    public Map<String, Object> getMemberList(HttpSession session, Integer groupId) {
        Map<String, Object> resultMap = new HashMap<>();
        User user = (User) session.getAttribute("user");
        if (user != null) {
            resultMap.put("groupMember", groupService.getGroupMemberList(groupId));
            resultMap.put("role", groupService.getMemberRole(user.getUserid(), groupId));
            return resultMap;
        }
        return null;
    }

    @GetMapping(path = "/removeMemberToGroup")
    public void removeMemberToGroup(HttpSession session, Integer memberId, Integer groupId) {
        User user = (User) session.getAttribute("user");
        if (user != null) {
            Integer userid = user.getUserid();
            groupService.removeGroupMember(groupId, userid, memberId);
        }
    }

    @GetMapping(path = "/quitGroup")
    public void quitGroup(Integer groupId, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user != null) {
            groupService.quitGroup(user.getUserid(), groupId);
        }
    }

    @GetMapping(path = "/deleteGroup")
    public void deleteGroup(Integer groupId, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user != null) {
            groupService.deleteGroup(groupId, user.getUserid());
        }
    }

    @GetMapping(path = "/modifyGroupName")
    public void modifyGroupName(Integer groupId, String groupName, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user != null) {
            groupService.updateGroupName(user.getUserid(), groupId, groupName);
        }
    }

    @GetMapping(path = "/createGroup")
    public void createGroup(String groupName, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user != null) {
            groupService.createGroup(user.getUserid(), groupName);
        }
    }
}
